package com.intelligent.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.intelligent.util.LambdaCacheHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

/**
 * MyBatis Plus 配置类
 * 解决lambda cache问题
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 由于主应用类已经配置了MybatisPlusInterceptor，这里不重复配置
     * 避免Bean重复定义
     */

    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        // 启用缓存，这有助于解决lambda cache问题

        globalConfig.setDbConfig(dbConfig);
        return globalConfig;
    }

    /**
     * 初始化时强制预加载实体类，解决lambda cache问题
     */
    @PostConstruct
    public void initLambdaCache() {
        try {
            // 强制加载实体类，确保lambda缓存正确初始化
            Class.forName("com.intelligent.entity.Outbound");
            Class.forName("com.intelligent.entity.OutboundDetail");
            Class.forName("com.intelligent.entity.Product");
            Class.forName("com.intelligent.entity.Unit");
            Class.forName("com.intelligent.entity.Warehouse");
            Class.forName("com.intelligent.entity.TUser");

            // 使用辅助工具初始化lambda缓存
            LambdaCacheHelper.initAllLambdaCache();

            System.out.println("Lambda cache entities preloaded successfully");
        } catch (Exception e) {
            System.err.println("Failed to preload entity classes: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
